const {Comment} = require("../model");
const blogServer = require('../server/blogServer'); 
const {avaterUrl} = require('../routerConfig')

const length = avaterUrl.length;

exports.delComment = async (_id)=>{

   const result = await Comment.deleteOne({_id});
    return result;
}

exports.getComment = async(_Id,limit,s)=>{
  
    let n = (s - 1) * limit;
    if(!_Id) return '_id不能为空！'; 
    const result = await Comment.find({blogId:_Id, isShow:true })
    .limit(limit)
    .skip(n);
     const count = await Comment.find({blogId:_Id, isShow:true}).count();
    return {
        row:result,
        count
    };
}

exports.addComment= async(blogId,name,content,blogTitle)=>{
    const  isBlog = await blogServer.findBlog(blogId);
    if(!(isBlog && name && content)) return '评论失败，必要条件缺失';
    const thumb =  avaterUrl[Math.floor(Math.random()*length)];
    const result = await Comment.create({
        blogId,
        name,
        content,
        thumb,
        blogTitle
    });
    return result;
}

exports.setCommentShow = async (_id,isShow) => {
    const result = await Comment.updateOne({_id},{
        $set:{
            isShow
        }
    })
    return result
}

exports.getCommentAdmin = async(limit = 10,s = 1)=>{
  
    let n = (s - 1) * limit; 
    const result = await Comment.find()
    .limit(limit)
    .skip(n);
     const count = await Comment.find().count();
    return {
        row:result,
        count
    };
}